package org.jb.coinche.client.business.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jb.coinche.client.business.model.Card.Color;
import org.jb.coinche.client.guistatemachine.AbstractState;

public class RoundTurn extends AbstractState implements GameState {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -312769390479189607L;
	private Round round;
	private Player currentPlayer;
	private Map< Player, Card > playedCards = new HashMap< Player, Card >();
	private Map< Player, Collection<Card> > playableCards = null;
	private Color asked = null;

	/**
	 * 
	 */
	public Game getGame() {
		return getRound().getGame();
	}
	
	/**
	 * @param round the round to set
	 */
	public void setRound(Round round) {
		this.round = round;
	}

	/**
	 * @return the round
	 */
	public Round getRound() {
		return round;
	}

	/**
	 * @param currentPlayer the currentPlayer to set
	 */
	public void setCurrentPlayer(Player currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	/**
	 * @return the currentPlayer
	 */
	public Player getCurrentPlayer() {
		return currentPlayer;
	}
	
	/**
	 * 
	 * @return
	 */
	public Color getAskedColor() {
		return asked;
	}
	
	/**
	 * 
	 * @param p
	 * @return
	 */
	public Card getPlayedCard( Player p )  {
		return playedCards.get( p );
	}
	
	/*
	 * 
	 */
	public boolean isFinished() {
		return playedCards.size() == 4;
	}
	
	/*
	 * 
	 */
	public void play( Player player, Card card ) {
		playedCards.put( player, card );
		
		if (asked == null)
			asked = card.getColor();
		
		currentPlayer = round.getGame().getNextPlayer( player );
	}

	/**
	 * @param playableCards the playableCards to set
	 */
	public void setPlayableCards(Map< Player, Collection<Card> > playableCards) {
		this.playableCards = playableCards;
	}

	/**
	 * @return the playableCards
	 */
	public Collection<Card> getPlayableCards( Player p ) {
		return playableCards == null ? null : playableCards.get(p);
	}

	/**
	 * 
	 * @return
	 */
	public Collection<Card> getPlayedCards() {
		return playedCards.values();
	}
}
